var app = getApp();
var ddapi = require('../../utils/md5.js');
var openid = wx.getStorageSync('openid');
var user_id = wx.getStorageSync('user_id');

Page({
  data: {
    gongxiang : [],
    shop_info: [],
    page: 1,
    page_count: 10,
    showModalStatus:true,
    shop_name:'',
    shop_id:'',
    city_id:'',
  },


  onShow: function () {
    var that = this
    var service = 'Product.GetGongXiangShop';
    var sign = ddapi.md5(openid + service + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=' + service,
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        user_id: user_id,
      },
      success: function (res) {
        var code = res.data.data.code;
        if (code == 0){
          that.setData({
            shop_info: res.data.data.info,
            shop_id:res.data.data.info[0].shop_id,
            city_id: res.data.data.info[0].city_id,
            shop_name: res.data.data.info[0].shop_name,
          });
          that.loadData();
        }
      }
    })
  },

  //查询共享商品列表
  loadData: function () {
    var that = this
    var shop_id = that.data.shop_id;
    var city_id = that.data.city_id;
    var service = 'Product.GetGongxiangNew';
    var page = that.data.page;
    var page_count = that.data.page_count;
    var sign = ddapi.md5(city_id + openid + page + page_count + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Product.GetGongxiangNew',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        openid: openid,
        user_id: user_id,
        shop_id: shop_id,
        city_id: city_id,
        page: page,
        page_count: page_count
      },

      success: function (res) {
        console.log(res)
        if (res.data.data.info.length > 0) {
          var info = that.data.gongxiang.concat(res.data.data.info)
          var loadmore = true;
        } else {
          var loadmore = false;
        }
        that.setData({
          shop_id: shop_id,
          city_id: city_id,
          gongxiang: info,
          loadmore: loadmore
        });
      }
    })
  },


  submitGx: function() {
    wx.navigateTo({
      url: "/pages/gxfabu/gxfabu"
    });
  },


  lqproduct: function (e) {
    var that = this
    console.log(e);
    
    var is_lingqu = e.currentTarget.dataset.is_lingqu;
    var key = e.currentTarget.dataset.key;
  
    var service = 'Product.SetGongxiangNew';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var product_id = e.currentTarget.dataset.product_id;
    var shop_id = that.data.shop_id;

    var sign = ddapi.md5(openid + product_id + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Product.SetGongxiangNew',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
        product_id: product_id,
        openid: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        var info = res.data.data.info;
        if (code == 0) {
          if (info.status == 1){
            wx.showModal({
              title: "提示",
              content: "领取成功",
              showCancel: false
            })
          } else if (info.status == 2){
            wx.showModal({
              title: "提示",
              content: "取消成功",
              showCancel: false
            })
          }
          var gongxiang = that.data.gongxiang;
          gongxiang[key].is_lingqu = is_lingqu;
          that.setData({
            gongxiang: gongxiang
          });
        } else {
          if (info.status == 3) {
            wx.showModal({
              title: "提示",
              content: "领取失败",
              showCancel: false
            })
          } else if (info.status == 4) {
            wx.showModal({
              title: "提示",
              content: "取消失败",
              showCancel: false
            })
          }
        }
      }
    })
  },

  //弹出隐藏控制功能
  choiceShopEject: function(e){
    var showModalStatus = e.currentTarget.dataset.type;
    if (showModalStatus){
      this.setData({
        showModalStatus:false
      })
    }else{
      this.setData({
        showModalStatus: true
      })
    }
  },

  //选择切换店铺数据
  switchShop: function(e){
    this.setData({
      shop_id: e.currentTarget.dataset.item.shop_id,
      city_id: e.currentTarget.dataset.item.city_id,
      shop_name: e.currentTarget.dataset.item.shop_name,
      showModalStatus: true,
      page: 1,
      page_count: 10,
      gongxiang: [],
    });
    this.loadData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.loadmore) {
      var page = Number(this.data.page) + Number(1);
      this.setData({
        page: page
      });
      this.loadData()
    }
  },

})